//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2002-2007
//
// Printing stuff.
// NOTE: the code is incomplete.
//

# include "StdAfx.h"
# include "Resource.h"
# include "Print.h"
# include "Map.h"
# include "MapView.h"
# include "WebMap.h"
# include "Position.h"
# include "Status.h"
# include "Wrappers.h"
# include "Globals.h"

static bool g_bPrint;
static HWND g_hdlgCancel;

static
BOOL CALLBACK PrintAbortProc (HDC _hdc, int _iCode) { 
	// Retrieve and remove messages from the thread's message queue. 
	MSG msg; 
	while (::PeekMessage (& msg, NULL, 0, 0, PM_REMOVE)) { 
		if (! ::IsDialogMessage (g_hdlgCancel, & msg)) { 
			::TranslateMessage (& msg); 
			::DispatchMessage (& msg); 
		} 
	} 

	return g_bPrint; 
}

static
INT_PTR CALLBACK AbortPrintJob (HWND _hWnd, UINT _msg, WPARAM _wParam, LPARAM _lParam) { 
	switch (_msg) {
		case WM_INITDIALOG:
            // Initialize the static text control. 
//           SetDlgItemText (_hWnd, IDD_FILE, ofn.lpstrFile); 
			return TRUE; 

		case WM_COMMAND:
			g_bPrint = false;
            return TRUE; 
	}

	return FALSE; 
} 

void PrintMap (HWND _hWnd) {
# ifndef _DEBUG
	ErrorMsgBox ("Sorry, the feature is not implemented.");
# else // _DEBUG

	//
	// Show Print dialog.
	//
	PRINTDLG pd;
	::memset (& pd, 0, sizeof (pd));
	pd.lStructSize = sizeof (pd);
	pd.hwndOwner = _hWnd;
	pd.nMinPage = 1;
	pd.nMaxPage = -1;
	pd.nFromPage = -1;
	pd.nToPage = -1;
	pd.nCopies  = 1;

//	pd.Flags = PD_RETURNDEFAULT;
//	::PrintDlg (& pd);
//	DEVMODE * pDevMode = (DEVMODE *) GlobalLock (pd.hDevMode);

	pd.Flags = PD_RETURNDC | PD_NOPAGENUMS | PD_NOSELECTION | PD_USEDEVMODECOPIESANDCOLLATE;
	if (::PrintDlg (& pd) == 0)
		return;
	::GlobalFree (pd.hDevMode);
	::GlobalFree (pd.hDevNames);

	// Get printer DC.
	const HDC hPrintDC = pd.hDC;
	CDCHolder hPrintDC_holder (hPrintDC);

	// Prepare Abort dialog.
	::SetAbortProc (hPrintDC, PrintAbortProc);
	g_hdlgCancel = CreateDlg (IDD_PRINT_ABORT, _hWnd, AbortPrintJob);
	::ShowWindow (g_hdlgCancel, SW_SHOW);

	DOCINFO di;
	::memset (& di, 0, sizeof (di));
	di.cbSize = sizeof (di);
	di.lpszDocName = g_strFileName;
	di.lpszOutput = NULL;
	::StartDoc (hPrintDC, & di);
	::StartPage (hPrintDC);

//	const int rc = ::GetDeviceCaps (hPrintDC, RASTERCAPS);
//	const bool bBitBlt = rc & RC_BITBLT;

	const int iLogPelsX_Printer = ::GetDeviceCaps (hPrintDC, LOGPIXELSX);
	const int iLogPelsY_Printer = ::GetDeviceCaps (hPrintDC, LOGPIXELSY);

	CClientDC hScreenDC (_hWnd);
    const int iLogPelsX_Screen  = ::GetDeviceCaps (hScreenDC, LOGPIXELSX);
    const int iLogPelsY_Screen  = ::GetDeviceCaps (hScreenDC, LOGPIXELSY);
 
	::SetMapMode (hPrintDC, MM_ISOTROPIC);
	::SetWindowExtEx   (hPrintDC, iLogPelsX_Screen,  iLogPelsY_Screen,  NULL);
	::SetViewportExtEx (hPrintDC, iLogPelsX_Printer, iLogPelsY_Printer, NULL);
/*
		RECT rect;
		rect.left = 0;
		rect.top = 0;
		rect.right = 200;
		rect.bottom = 200;
		::FillRect (hDC, & rect, g_hbrTmp);
*/
	//
	// Render the map.
	//
	SetStatus (GetStr (IDS_STATUS_PRINTING));
	SetProgressStatus (0);
	{
		CCritSec::CLock lock (g_cs);

		rect_t rectVisible;
		GetVisibleRect (rectVisible);

		if (g_bShowGoogleMaps)
			DrawWebMap (hPrintDC, rectVisible);
		g_map.Draw (hPrintDC, rectVisible, 0, 100, g_bShowAttachments);
	}
	SetStatusReady ();

	::EndPage (hPrintDC);
	::EndDoc (hPrintDC);

	::DestroyWindow (g_hdlgCancel); 
# endif // _DEBUG
}

void PrintSetup (HWND _hWnd) {
	// TODO:
	ErrorMsgBox ("Sorry, the feature is not implemented.");
}